package net.ojbk.study.twobox.service;

import com.github.pagehelper.PageInfo;
import net.ojbk.study.twobox.beans.constraint.Constraint;
import net.ojbk.study.twobox.beans.po.Department;
import net.ojbk.study.twobox.beans.po.Post;
import net.ojbk.study.twobox.dao.PostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import tk.mybatis.mapper.common.Mapper;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/4/12
 * Time: 10:08
 * Description: No Description
 */
@Service
public class PostService extends SupperService<Post> {
    @Autowired
    Mapper<Department> departmentMapper;

    @Autowired
    PostMapper postMapper;
    /**
     * 增
     *
     * @param o
     * @param br
     */
    @Override
    public void addOne(Post o, BindingResult br) throws Exception {
        this.validated(br, Post.class);
        // 先查询是否有该id的部门
        Department department = new Department();
        department.setId(o.getDepid());
        // TODO TEST
        int count = departmentMapper.selectCount(department);
        if (count <= 0)
            throw new Exception("此部门不存在。");
        o.setId(null);
        dao.insert(o);
    }

    /**
     * 删
     *
     * @param id
     */
    @Override
    public void deleteOne(int id) throws Exception {
        int staffCount = postMapper.getStaffCountByKey(id);
        if (staffCount > 0)
            throw new Exception("此职务下面还有人！");
        postMapper.deleteByPrimaryKey(id);
    }

    /**
     * 改
     *
     * @param o
     * @param br
     */
    @Override
    public void updateOne(Post o, BindingResult br) throws Exception {
        this.validated(br, Post.class);
        dao.updateByPrimaryKeySelective(o);
    }
}
